﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DigitalRune.Windows.Docking;
using md.imi.membranes.objects.rules;

namespace md.imi.membranes.gui.Editor
{
    public partial class RulesEditor : DockableForm
    {
        private Rule mRule;

        public RulesEditor()
        {
            InitializeComponent();
        }

        public RulesEditor(string membraneLabel) : this ()
        {
            Text = "Edit Rule for Membrane " + membraneLabel;
        }

        public void LoadData(Rule rule)
        {
            mRule = rule;

            if (mRule != null)
            {
                cbRuleType.Enabled          = true;
                gbRightHandRules.Enabled    = true;
                gbRigthHandRules.Enabled    = true;

                tbLabel.Text = mRule.Label;
                if (mRule is ReplicationRule)
                {
                    if ((mRule as ReplicationRule).LeftHandRule.SubstringPattern != null)
                    {
                        tbLeftHandRule.Text = (mRule as ReplicationRule).LeftHandRule.SubstringPattern.ToString();
                    }
                    cbRuleType.SelectedIndex = 0;
                    ReloadRigthHandRules();
                }
                else
                {
                    cbRuleType.Enabled       = false;
                    gbRightHandRules.Enabled = false;
                    gbRigthHandRules.Enabled = false;
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (mRule is ReplicationRule)
            {
                var msEditor = new MultisetEditor("");
                msEditor.SetInfoText("Edit Substring pattern for LeftHand rule " + mRule.Label);
                msEditor.LoadData((mRule as ReplicationRule).LeftHandRule.SubstringPattern);
                if (msEditor.ShowDialog() == DialogResult.OK)
                {
                    (mRule as ReplicationRule).LeftHandRule.SubstringPattern = msEditor.GetData();
                    tbLeftHandRule.Text = (mRule as ReplicationRule).LeftHandRule.SubstringPattern.ToString();
                }
            }

        }

        private void RightHandRulesActions(object sender, EventArgs e)
        {
            if (sender == tsEditRule && lbRigthHandRulesList.SelectedItem != null)
            {
                var rhrEdit = new RightHandReplicationRuleEdit(mRule.Label,
                    ((ReplicationRule) mRule).RightHandRules[lbRigthHandRulesList.SelectedIndex],
                    ((ReplicationRule) mRule).ParentMembrane
                   );

                if (rhrEdit.ShowDialog() == DialogResult.OK)
                {
                    ((ReplicationRule) mRule).RightHandRules[
                        lbRigthHandRulesList.SelectedIndex] = rhrEdit.GetData();

                    ReloadRigthHandRules();
                }
            } 
            else if (sender == tsAddRule)
            {
                var rhrEdit = new RightHandReplicationRuleEdit(mRule.Label, new RightHandRule(),
                                    ((ReplicationRule) mRule).ParentMembrane);

                if (rhrEdit.ShowDialog() == DialogResult.OK)
                {
                    ((ReplicationRule)mRule).RightHandRules.Add(rhrEdit.GetData());

                    ReloadRigthHandRules();
                }
            }
            else if (sender == tsRemoveRule && lbRigthHandRulesList.SelectedItem != null)
            {
                ((ReplicationRule) mRule).RightHandRules.RemoveAt(lbRigthHandRulesList.SelectedIndex);
                ReloadRigthHandRules();
            }
        }

        private void ReloadRigthHandRules ()
        {
            lbRigthHandRulesList.Items.Clear();
            if (mRule != null && mRule is ReplicationRule)
            {
                var replicationRule = (ReplicationRule) mRule;
                if (replicationRule.RightHandRules != null)
                {
                    foreach (var rule in  replicationRule.RightHandRules)
                    {
                        lbRigthHandRulesList.Items.Add(rule.ToString());
                    }
                } 

            }
        }

        public Rule GetData()
        {
            mRule.Label = tbLabel.Text;

            if (cbRuleType.SelectedIndex == 0)
            {
                // var replicationRule = (ReplicationRule) mRule;
            }

            return mRule;
        }

        private void lbRigthHandRulesList_DoubleClick(object sender, EventArgs e)
        {
            RightHandRulesActions(tsEditRule, null);

        }
    }
}
